package cn.com.css.taxInspection.attention.controller;

import cn.com.css.taxInspection.attention.entity.AjcxRecord;
import cn.com.css.taxInspection.attention.entity.CaseAjxxRelation;
import cn.com.css.taxInspection.attention.entity.JaajxxYjqlsjEntity;
import cn.com.css.taxInspection.attention.service.CaseAjxxRelationService;
import cn.com.css.taxInspection.attention.service.JaajxxYjqlsjService;
import cn.com.css.taxInspection.common.aop.annotation.LogAnnotation;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * ${comments}
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-04-18 16:07:07
 */
@Controller
@RequestMapping("/")
public class JaajxxYjqlsjController {
    @Resource
    private HttpSessionService httpSessionService;

    @Autowired
    private JaajxxYjqlsjService jaajxxYjqlsjService;
    @Resource
    private CaseAjxxRelationService caseAjxxRelationService;

    @ApiOperation(value = "删除")
    @DeleteMapping("jaajxxYjqlsj/delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        jaajxxYjqlsjService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("jaajxxYjqlsj/update")
    @ResponseBody
    public DataResult update(@RequestBody JaajxxYjqlsjEntity jaajxxYjqlsj){
        jaajxxYjqlsjService.updateById(jaajxxYjqlsj);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("jaajxxYjqlsj/listByPage")
    @ResponseBody
    public DataResult findListByPage(@RequestBody JaajxxYjqlsjEntity req) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Page page = new Page(req.getPage(), req.getLimit());
        LambdaQueryWrapper<JaajxxYjqlsjEntity> queryWrapper = Wrappers.lambdaQuery();
        if(!StringUtils.isEmpty(req.getNsrsbh())){
            queryWrapper.eq(JaajxxYjqlsjEntity::getNsrsbh, req.getNsrsbh());
        }
        if(!StringUtils.isEmpty(req.getNsrmc())){
            queryWrapper.eq(JaajxxYjqlsjEntity::getNsrmc, req.getNsrmc());
        }
        if(!StringUtils.isEmpty(req.getAjbh())){
            queryWrapper.eq(JaajxxYjqlsjEntity::getAjbh, req.getAjbh());
        }
        if(!StringUtils.isEmpty(req.getStartTime())){
            queryWrapper.ge(JaajxxYjqlsjEntity::getYjqlTime, sdf.parse(req.getStartTime()));
            queryWrapper.le(JaajxxYjqlsjEntity::getYjqlTime, sdf.parse(req.getEndTime()));
        }
        //查询当前登录人所在的税务机关的数据
        String currentSwjgDm = httpSessionService.getCurrentDeptNo();
        String swjgDmTmp = currentSwjgDm.substring(0,9);
        queryWrapper.likeRight(JaajxxYjqlsjEntity::getJcks,swjgDmTmp);
        queryWrapper.eq(JaajxxYjqlsjEntity::getIsdelete,'N');
        IPage<JaajxxYjqlsjEntity> iPage = jaajxxYjqlsjService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }


    @ApiOperation(value = "更新")
    @PutMapping("jaajxxYjqlsj/plupdateyjqltime")
    @ResponseBody
    public DataResult plupdateyjqltime(@RequestBody List<JaajxxYjqlsjEntity> jaajxxYjqlsjs){
        List<String> ids  = new ArrayList<>();
        for(JaajxxYjqlsjEntity obj:jaajxxYjqlsjs){
            ids.add(obj.getId());
        }
        UpdateWrapper<JaajxxYjqlsjEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);
        updateWrapper.set("YJQL_TIME", jaajxxYjqlsjs.get(0).getYjqlTime());
        updateWrapper.set("UPDATE_TIME", new Date());
        updateWrapper.set("XGR_DM", httpSessionService.getCurrentUserId());
        jaajxxYjqlsjService.update(updateWrapper);
        return DataResult.success();
    }
}
